dedecms重新定义cn_substr函数截取字数准确
* @param int $cutLen 截取的长度
* @access public
*/
二、全站都使用cn_substr()函数,不管你程序是gbk还是utf8;
dedecms的cn_substr()和cn_substr_utf8()截取的字符串ms不准,平时也用习惯cn_substr(),也不愿用什么cn_substr_utf8()今天弄了下,现在还是比较准了。按照一个汉字2个字节调用就行了!
* 中英文截取字符串,汉字安2个字节* @param string $oDot 截取后加的字符串,如经常用的三个点
方法说明:
* @param bool $addSlashes 是加\
if ( ! function_exists(‘cn_substr’)){
function cn_substr($str, $cutLen, $oDot = null, $hasHtml = false, $cutSlashes = false, $addSlashes = false) {
global $cfg_soft_lang;
$str = trim ( $str );
if ($cutSlashes) $str = stripslashes ( $str );
if($hasHtml){
$str = preg_replace ( “/(\<[^\<]*\>|\r|\n|\s|\[.+?\])/is”, ‘ ‘, $str );
$str = htmlspecialchars ( $str );
}else{
$str = htmlspecialchars ( $str );
}
if ($cutLen && strlen ( $str ) > $cutLen) {
$nStr = ”;
if ($cfg_soft_lang == ‘utf-8′) {
$n = 0;
$tn = 0;
$noc = 0;
while ( $n < strlen ( $str ) ) {
$t = ord ( $str [$n] );
if ($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1;
$n ++;
$noc ++;
} elseif (194 <= $t && $t <= 223) {
$tn = 2;
$n += 2;
$noc += 2;
} elseif (224 <= $t && $t < 239) {
$tn = 3;
$n += 3;
$noc += 2;
} elseif (240 <= $t && $t <= 247) {
$tn = 4;
$n += 4;
$noc += 2;
} elseif (248 <= $t && $t <= 251) {
$tn = 5;
$n += 5;
$noc += 2;
} elseif ($t == 252 || $t == 253) {
$tn = 6;
$n += 6;
$noc += 2;
} else {
$n ++;
}
if ($noc >= $cutLen)break;
}
if ($noc > $cutLen) $n -= $tn;
$nStr = substr ( $str, 0, $n );
} else {
for($i = 0; $i < $cutLen – 1; $i ++) {
if (ord ( $str [$i] ) > 127) {
$nStr .= $str [$i] . $str [$i + 1];
$i ++;
} else {
$nStr .= $str [$i];
}
}
}
$str = $nStr . $oDot;
}
if ($addSlashes) $str = addslashes ( $str );
$str = htmlspecialchars_decode ( $str );
return trim ( $str );
}
}
一、找到\include\helpers\string.helper.php把原来约33到102行(也就是定义cn_substr()函数的那段代码)替换掉,你要是怕不行,可以先把这个文件备份下,亲;
Copy to Clipboard

* @param string $str 需要截取的字符串
比如你要调用10个字(拼音汉字混杂):[field:title function='cn_substr(@me,20)']即可
/**
引用的内容:[]
相关热词: Dedecms dedecms修改内容 dede
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/jz/dedecms/4099.shtml
相关文章
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
dedecms上传透明配景图片酿
时间:2021-01-12
-
织梦DEDECMS V6安装后报错打
时间:2021-01-12
-
保障Dede生态的安全、完整
时间:2021-01-12
-
织梦官方将通过赠送商业
时间:2021-01-12
-
则需要先导入数据
时间:2021-01-12
-
在点击系统设置里面的图
时间:2021-01-12
-
利用织梦cms做多语言的网
时间:2021-01-12
-
DEDECMS登录后台慢的完美解
时间:2021-01-12
热门文章
-
DedeCMS站点高级安全策略(Linux篇)
时间:2020-12-16
-
阿里云主机安装织梦系统出现data无写入权
时间:2020-12-17
-
织梦教程:织梦怎么整合Discuz站点
时间:2020-12-14
-
织梦批量删除文章内容数据库里所有超链
时间:2020-12-17
-
织梦dedecms系统UTF8版编辑器中多图发布按
时间:2020-12-16
-
织梦更换UEditor编辑器后栏目内容、单页无
时间:2020-12-17
-
利用织梦cms做多语言的网站(图解)
时间:2021-01-12
-
织梦关键词自动添加链接设置方法
时间:2020-12-15
-
如何防止别人盗用你的织梦模板
时间:2020-12-14
-
织梦dedecms熊掌号时间因子改造教程
时间:2020-12-13
